package com.rency.mtds.starter.client.advice;

import com.rency.mtds.starter.client.annotation.MTDS;
import com.rency.mtds.starter.client.holder.DataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

/**
 * @author: caiyu.ren
 * Created on 2019/9/3 9:17
 * @version: V1.0.0
 * @since JDK 11
 */
@Aspect
@Order(-10)
@Slf4j
public class DataSourceRouteAdvice {

    @Before("@annotation(mtds)")
    public void routeDataSource(JoinPoint point, MTDS mtds) {
        log.debug("halo dynamic data source key:{}",mtds.value());
        DataSourceContextHolder.setDataSourceLookupKey(mtds.value());
    }

    @After("@annotation(mtds)")
    public void clearDataSource(JoinPoint point, MTDS mtds) {
        DataSourceContextHolder.clearDataSourceLookupKey();
    }
}
